package com.common.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.common.user.domain.Permission;
import com.common.user.repository.PermissionRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * Permission api
 * @author enbool
 *
 */
@Api(description = "Manage Permission")
@RestController
@RequestMapping(value = "/permissions")
public class PermissionController {
	
	@Autowired
	PermissionRepository permissionRepository;
	
	@ApiOperation(value = "Create a permission")
	@PostMapping
	public Permission create(@RequestBody Permission permission) {
		
		return permissionRepository.save(permission); 
	}
	
	@ApiOperation(value = "Delete a permission")
	@ApiImplicitParam(name = "id", value = "The id of the permission to delete.")
	@DeleteMapping(value = "/{id:\\d+}")
	public void delete(@PathVariable Long id) {
		
		permissionRepository.delete(id);
	}
	
	@ApiOperation(value = "Update a permission")
	@PutMapping
	public Permission update(@RequestBody Permission permission) {
		
		return permissionRepository.save(permission);
	}
	
	@ApiOperation(value = "List of permissions")
	@GetMapping
	public List<Permission> findAll() {
		
		return permissionRepository.findAll();
	}
	
	@ApiOperation(value = "Get a permission")
	@ApiImplicitParam(name = "id", value = "The id of the permission to get.")
	@GetMapping(value = "/{id:\\d+}")
	public Permission findOne(@PathVariable Long id) {
		
		return permissionRepository.getOne(id);
	}
}
